package com.abl.common.util;

import java.util.Iterator;
import java.util.Objects;

public class StringKit {

    public static String subBefore(String content, String finStr) {
        if (isBlank(content) || isBlank(finStr) || !content.contains(finStr)) {
            return null;
        }
        int index = content.indexOf(finStr);
        //        System.out.println(content);
        //        System.out.println(index);
        //        System.out.println(content.substring(0, index));
        return content.substring(0, index);
    }

    public static String subAfter(String content, String finStr) {
        if (isBlank(content) || isBlank(finStr) || !content.contains(finStr)) {
            return null;
        }
        int index = content.indexOf(finStr);
        //        System.out.println(content);
        //        System.out.println(index);
        //        System.out.println(content.substring(index + finStr.length()));
        return content.substring(index + finStr.length());
    }

    //copy to org.apache.commons
    public static boolean isBlank(CharSequence cs) {
        int strLen = length(cs);
        if (strLen != 0) {
            for (int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(cs.charAt(i))) {
                    return false;
                }
            }

        }
        return true;
    }

    //copy to org.apache.commons
    public static int length(CharSequence cs) {
        return cs == null ? 0 : cs.length();
    }

    //copy to org.apache.commons
    public static boolean isNotBlank(CharSequence cs) {
        return !isBlank(cs);
    }

    //copy to org.apache.commons
    public static String join(Iterable<?> iterable, String separator) {
        return iterable == null ? null : join(iterable.iterator(), separator);
    }

    //copy to org.apache.commons
    public static String join(Iterator<?> iterator, String separator) {
        if (iterator == null) {
            return null;
        } else if (!iterator.hasNext()) {
            return "";
        } else {
            Object first = iterator.next();
            if (!iterator.hasNext()) {
                return Objects.toString(first, "");
            } else {
                StringBuilder buf = new StringBuilder(256);
                if (first != null) {
                    buf.append(first);
                }

                while (iterator.hasNext()) {
                    if (separator != null) {
                        buf.append(separator);
                    }

                    Object obj = iterator.next();
                    if (obj != null) {
                        buf.append(obj);
                    }
                }

                return buf.toString();
            }
        }
    }

    public static boolean isNull(Object object) {
        return object == null;
    }
}
